#include "IndexBuffer.h"

#include <glad/glad.h>
#include <cassert>

IndexBuffer::IndexBuffer(const unsigned int *data, unsigned int count) : m_indexCount(count)
{
    // 生成缓冲区对象名称
    glGenBuffers(1, &m_rendererId);
    // 绑定一个已命名的缓冲区对象
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_rendererId);
    // 创建并初始化缓冲区对象的数据存储
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW);
}

IndexBuffer::~IndexBuffer()
{
    glDeleteBuffers(1, &m_rendererId);
}

void IndexBuffer::bind() const
{
    // 绑定一个已命名的缓冲区对象
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_rendererId);
}

void IndexBuffer::unbind() const
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}